package cn.org.timespace.easyframe.util;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
/**
 * 获取 spring 中的 bean
 * 
 * 要使用此类 必须在 applicationContext.xml 中 添加 
 * <bean id="beanFactory" class="com.student.util.SpringBeanFactory" scope="singleton"/>
 * */
public class SpringBeanFactory implements BeanFactoryAware{ 
	
	private static BeanFactory beanFactory = null;
	private static SpringBeanFactory serviceFactory = null;
	
	
	public void setBeanFactory(BeanFactory factory) throws BeansException {
		this.beanFactory = factory;
	}
	public BeanFactory getBeanFactory(){
		return beanFactory;
	}
	
	public static SpringBeanFactory getInstance(){
		if(serviceFactory == null)
			serviceFactory = (SpringBeanFactory)beanFactory.getBean("beanFactory");
		return serviceFactory;
	}
	/**
	 * @param String beanName notNull 要获取的bean 的名称  
	 * */
	public Object getBean(String beanName){
		return beanFactory.getBean(beanName);
	}
}
